package com.cardmanager.pro.executor;

import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.cardmanager.pro.config.PropertiesConfig;
import com.cardmanager.pro.util.CardConstss;
import com.cardmanager.pro.util.RestTemplateUtil;

import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.ExceptionUtil;
import net.sf.json.JSONObject;
@Component
public class BaseExecutor {

	private final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private RestTemplateUtil util;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private PropertiesConfig propertiesConfig;
	
	
	public JSONObject addCreditCardOrder(
			String userId, String rate, String type, String amount, String realAmount,
			String creditCardNumber, String channelTag, String orderCode, String serviceCharge, String description,
			String remark) throws RuntimeException{
//		RestTemplate restTemplate = new RestTemplate();
//		URI uri = util.getServiceUrl("transactionclear", "error url request");
		String url = "http://transactionclear/v1.0/transactionclear/payment/add/credit/card/manager/order";
		LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userId", userId);
		requestEntity.add("rate",rate );
		requestEntity.add("type", type);
		requestEntity.add("amount",amount );
		requestEntity.add("realAmount",realAmount );
		requestEntity.add("channelTag",channelTag );
		requestEntity.add("orderCode",orderCode );
		requestEntity.add("serviceCharge", serviceCharge);
		requestEntity.add("description", description);
		requestEntity.add("remark", remark);
		requestEntity.add("creditCardNumber", creditCardNumber);
		JSONObject resultJSONObject;
		System.out.println(requestEntity);
		try {
			String resultString = restTemplate.postForObject(url, requestEntity, String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
			throw new RuntimeException(e);
		}
		return resultJSONObject;
	}
	
	
	public JSONObject paymentTopupRequest(
			String amount,String ordercode,String orderdesc,String userid,String brandcode,
			String channel_type,String channel_tag,String extra
			) throws RuntimeException{
//		RestTemplate restTemplate = new RestTemplate();
//		URI uri = util.getServiceUrl("paymentchannel", "error url request");
		String url = "http://paymentchannel/v1.0/paymentchannel/topup/request";
		LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userid", userid);
		requestEntity.add("brandcode",brandcode );
		requestEntity.add("channel_type", channel_type);
		requestEntity.add("amount",amount );
		requestEntity.add("channel_tag",channel_tag );
		requestEntity.add("ordercode",ordercode );
		requestEntity.add("orderdesc", orderdesc);
		requestEntity.add("extra", extra);
		JSONObject resultJSONObject;
		try {
			String resultString = restTemplate.postForObject(url, requestEntity, String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
			throw new RuntimeException(e);
		}
		return resultJSONObject;
	}
	
	public JSONObject getUserInfo(String userId) throws RuntimeException{
//		RestTemplate restTemplate = new RestTemplate();
//		URI uri = util.getServiceUrl("user", "error url request");
		String url = "http://user/v1.0/user/find/by/userid";
		LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userId", userId);
		JSONObject resultJSONObject;
		try {
			String resultString = restTemplate.postForObject(url, requestEntity, String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
			throw new RuntimeException(e);
		}
		return resultJSONObject;
		
	}
	
	public JSONObject getOrderStatusByVersion(String orderCode,String orderType,String version)throws RuntimeException{
		String url = null;
		JSONObject resultJSONObject = null;
		String resultString = null;
		String type = null;
		if(CardConstss.CARD_VERSION_1.equals(version)){
			url = propertiesConfig.getPaymentgatewayIp() + "/v1.0/paymentgateway/topup/wf/ordercodequery";
			if (CommonConstants.ORDER_TYPE_CONSUME.equals(orderType)) {
				type = "fastPay";
			}else if (CommonConstants.ORDER_TYPE_REPAYMENT.equals(orderType)) {
				type = "transfer";
			}
			try {
				LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
				requestEntity.add("orderCode", orderCode);
				requestEntity.add("transType", type);
				resultString = new RestTemplate().postForObject(url,requestEntity,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}else if(CardConstss.CARD_VERSION_2.equals(version)){
			url = "http://paymentchannel/v1.0/paymentchannel/topup/hljc/ordercodequery";
			try {
				resultString = restTemplate.getForObject(url + "?ordercode="+orderCode,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}else if (CardConstss.CARD_VERSION_3.equals(version)) {
			if (CommonConstants.ORDER_TYPE_CONSUME.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/cjhk/ordercodequery";
			}else if (CommonConstants.ORDER_TYPE_REPAYMENT.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/cjhk/transferquery";
			}
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("ordercode", orderCode);
			try {
				resultString = restTemplate.postForObject(url,requestEntity,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}else if (CardConstss.CARD_VERSION_4.equals(version)) {
			if (CommonConstants.ORDER_TYPE_CONSUME.equals(orderType)) {
				url ="http://paymentchannel/v1.0/paymentchannel/topup/wmyk/fastpayquery";
			}else if (CommonConstants.ORDER_TYPE_REPAYMENT.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/wmyk/transferquery";
			}
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("ordercode", orderCode);
			try {
				resultString = restTemplate.postForObject(url,requestEntity,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}else if (CardConstss.CARD_VERSION_5.equals(version)) {
			if (CommonConstants.ORDER_TYPE_CONSUME.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/cjquick/ordercodequery";
			}else if (CommonConstants.ORDER_TYPE_REPAYMENT.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/cjquick/transferquery";
			}
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("ordercode", orderCode);
			try {
				resultString = restTemplate.postForObject(url,requestEntity,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}else if (CardConstss.CARD_VERSION_6.equals(version)) {
			if (CommonConstants.ORDER_TYPE_CONSUME.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/wmyknew/orderquery";
			}else if (CommonConstants.ORDER_TYPE_REPAYMENT.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/wmyknew/transferquery";
			}
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("orderCode", orderCode);
			try {
				resultString = restTemplate.postForObject(url,requestEntity,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}else if (CardConstss.CARD_VERSION_7.equals(version)) {
			if (CommonConstants.ORDER_TYPE_CONSUME.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/ybhk/ordercodequery";
			}else if (CommonConstants.ORDER_TYPE_REPAYMENT.equals(orderType)) {
				url = "http://paymentchannel/v1.0/paymentchannel/topup/ybhk/transferquery";
			}
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("orderCode", orderCode);
			try {
				resultString = restTemplate.postForObject(url,requestEntity,String.class);
				resultJSONObject = JSONObject.fromObject(resultString);
			} catch (Exception e) {
				e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
				throw new RuntimeException(e);
			}
		}
		return resultJSONObject;
	}
	
	public JSONObject updatePaymentOrderByOrderCode(String orderCode){
		String url = "http://transactionclear/v1.0/transactionclear/payment/update";
		JSONObject resultJSONObject;
		try {
			LinkedMultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
			requestEntity.add("order_code", orderCode);
			requestEntity.add("status", "1");
			String resultString = restTemplate.postForObject(url,requestEntity,String.class);
			resultJSONObject = JSONObject.fromObject(resultString);
		} catch (Exception e) {
			e.printStackTrace();LOG.error(ExceptionUtil.errInfo(e));
			throw new RuntimeException(e);
		}
		return resultJSONObject;
	}
}
